package com.yantong.controller;

import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.AchieveConfig;
import com.yantong.service.AchieveConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 业绩配置控制
 * @author kanghaixin
 * @date 2018/9/20.17:07
 */
@Controller
@RequestMapping("/achieve/config")
public class AchieveConfigController extends BaseController {

    private static final String prefix = "system/achieve";
    @Autowired
    private AchieveConfigService achieveConfigService;

    /**
     * 列表页面
     * @param model
     * @return
     */
    @GetMapping()
    @RequiresPermissions("achieve:config:config")
    public String achieveConfigs(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/achieveConfig";
    }

    /**
     * 列表数据
     * @param centreId
     * @param offset
     * @param limit
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("achieve:config:config")
    public PageUtils<AchieveConfig> listAchieveConfigs(Long centreId, int offset, int limit) {
        centreId = centreId == null ? getUserCentreId(): centreId;
        return achieveConfigService.listAchieveConfigs(centreId, offset, limit);
    }


    /**
     * 添加界面
     * @param model
     * @return
     */
    @RequiresPermissions("achieve:config:add")
    @GetMapping("/add")
    public String add(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix +"/add";
    }

    /**
     * 保存配置
     * @param config
     * @return
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("achieve:config:add")
    public ResultVo save(AchieveConfig config) {
        if (config == null) {
            return ResultVo.error();
        }
        if (config.getCentreId() == null) {
            config.setCentreId(getUserCentreId());
        }
        if (achieveConfigService.save(config)< 1){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 编辑页面
     * @param model
     * @param configId
     * @return
     */
    @RequiresPermissions("achieve:config:edit")
    @GetMapping("/edit/{configId}")
    public String edit(Model model, @PathVariable Long configId) {
        model.addAttribute("config", achieveConfigService.getByKey(configId));
        return prefix + "/edit";
    }

    /**
     * 编辑保存
     * @param config
     * @return
     */
    @RequiresPermissions("achieve:config:edit")
    @ResponseBody
    @PostMapping("/update")
    public ResultVo updateConfig(AchieveConfig config) {
        if (config == null || config.getId() == null) {
            return ResultVo.error();
        }
        if (achieveConfigService.updateConfig(config)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 移除配置
     * @param configId
     * @return
     */
    @RequiresPermissions("achieve:config:remove")
    @ResponseBody
    @PostMapping("/remove")
    public ResultVo remove(Long configId) {
        if (configId == null) {
            return ResultVo.error();
        }
        if (achieveConfigService.remove(configId) < 1){
            return ResultVo.error();
        }
        return ResultVo.ok();
    }


}
